package ckw.gui.wizards;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import ckw.database.OKW;

public class WzdElectionDlgOKWSelect extends Dialog {
	private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
		public String getColumnText(Object element, int columnIndex) {
			return ((OKW)element).getName() + " - " + ((OKW)element).getIdentificationField();
		}
	}
	private ArrayList<OKW> okwList;
	private CheckboxTableViewer checkboxTableViewer;
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public WzdElectionDlgOKWSelect(Shell shell, ArrayList<OKW> selectedOkwList) {
		super(shell);
		this.okwList = selectedOkwList;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		
		Label lblWybierz = new Label(container, SWT.NONE);
		lblWybierz.setText("Wybierz OKW:");
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
		Table table = checkboxTableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		checkboxTableViewer.setLabelProvider(new TableLabelProvider());
		checkboxTableViewer.addCheckStateListener(new ICheckStateListener() {
			@Override
			public void checkStateChanged(CheckStateChangedEvent arg0) {
				((OKW) arg0.getElement()).toggle();
			}
		});
		
		// Wypelnienie tabeli OKW
		for(OKW okw : okwList) {
			checkboxTableViewer.add(okw);
			if(okw.getChecked()) {
				checkboxTableViewer.setChecked(okw, true);
			}
		}
		
		return container;
	}

	public ArrayList<OKW> getSelectedOkwList() {
		return okwList;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

}
